package com.dhcc.bpm.modules.scf.service.HomePageService.HomePageImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.modules.scf.entity.HomePage.*;
import com.dhcc.bpm.modules.scf.mapper.HomePageMapper.ScfHomePageMapper;
import com.dhcc.bpm.modules.scf.service.HomePageService.IScfHomePageService;
import com.dhcc.bpm.modules.scf.util.UnitConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ScfHomePageServiceImpl extends ServiceImpl<ScfHomePageMapper, ScfHomePage> implements IScfHomePageService {
    @Autowired
    private UnitConverter unitConverter;

    @Override
    public String selectSubjectCounts(String[] orgCodes) {
        return baseMapper.selectSubjectCounts(  Arrays.asList(orgCodes)  );
    }

    @Override
    public List<ScfSubjectAddress> selectSubjectAddress(String[] orgCodes) {
        return baseMapper.selectSubjectAddress(Arrays.asList(orgCodes) );
    }

    @Override
    public List<ScfSubjectCount> selectSubjectCount(String[] orgCodes) {
        return baseMapper.selectSubjectCount( Arrays.asList(orgCodes)  );
    }

    @Override
    public List<ScfSubjectCount> selectSubjectCountByMonth(String[] orgCodes) {
        return baseMapper.selectSubjectCountByMonth(Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectDyCj( String[] orgCodes ) {
        return baseMapper.selectDyCj(    Arrays.asList(orgCodes) );
    }

    /**
     * 商品成交列表
     *货品分类 百分比
     * @param page
     * @param scfGoodsWeightCount
     * @return
     */
    @Override
    public IPage<ScfGoodsWeightCount> selectGoodsWeightCount(IPage<ScfGoodsWeightCount> page, ScfGoodsWeightCount scfGoodsWeightCount) {
        List<ScfGoodsWeightCount> scfGoodsWeightCounts = baseMapper.selectGoodsWeightCount(page, scfGoodsWeightCount);

        for (ScfGoodsWeightCount goodsWeightCount : scfGoodsWeightCounts) {

            // 货品成交吨数--单位换算为万吨
            goodsWeightCount.setGoodsWeight(unitConverter.convertToMetricTons(goodsWeightCount.getGoodsWeight()));
            // 货品累计碳排放--单位换算为万吨
            goodsWeightCount.setGoodsCarbonEmissions(unitConverter.convertToMetricTons(goodsWeightCount.getGoodsCarbonEmissions()));
        }
        return page.setRecords(scfGoodsWeightCounts);
    }

    @Override
    public ScfHomePage selectDnCj(String[] orgCodes) {
        return baseMapper.selectDnCj( Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectLjCj(String[] orgCodes) {
        return baseMapper.selectLjCj(Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectDnSalesCount(String[] orgCodes ) {
        return baseMapper.selectDnSalesCount( Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectDnWeight(String[] orgCodes) {
        return baseMapper.selectDnWeight(Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectLjSalesCount(String[] orgCodes) {
        return baseMapper.selectLjSalesCount(Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectLjWeight(String[] orgCodes) {
        return baseMapper.selectLjWeight(Arrays.asList(orgCodes) );
    }

    @Override
    public ScfHomePage selectCarbonEmissions(String[] orgCodes) {
        return baseMapper.selectCarbonEmissions(Arrays.asList(orgCodes) );
    }

    /**
     * 实时成交列表
     *
     * @param page
     * @param scfSelRealtime
     * @return
     */
    @Override
    public IPage<ScfSelRealtime> selectSelRealtime(IPage<ScfSelRealtime> page, ScfSelRealtime scfSelRealtime) {
        List<ScfSelRealtime> scfSelRealtimes = baseMapper.selectSelRealtime(page, scfSelRealtime);
        for (ScfSelRealtime selRealtime : scfSelRealtimes) {
            selRealtime.setCarbonEmissions(unitConverter.convertToMetricTons(selRealtime.getCarbonEmissions()));
        }
        return page.setRecords(scfSelRealtimes);
    }

    /**
     * 再生资源企业成交排名
     *
     * @param page
     * @param scfSelRenewableRanking
     * @return
     */
    @Override
    public IPage<ScfSelRenewableRanking> selectSelRenewableRanking(IPage<ScfSelRenewableRanking> page, ScfSelRenewableRanking scfSelRenewableRanking) {
        List<ScfSelRenewableRanking> scfSelRenewableRankings = baseMapper.selectSelRenewableRanking(page, scfSelRenewableRanking);
        for (ScfSelRenewableRanking selRenewableRanking : scfSelRenewableRankings) {
            // 再生成交吨数--单位换算为万吨
            selRenewableRanking.setRenewableWeight(unitConverter.convertToMetricTons(selRenewableRanking.getRenewableWeight()));
            // 再生碳排放量--单位换算为万吨
            selRenewableRanking.setCarbonEmissions(unitConverter.convertToMetricTons(selRenewableRanking.getCarbonEmissions()));

        }
        return page.setRecords(scfSelRenewableRankings);
    }

    /**
     * 利废企业成交排名
     *
     * @param page
     * @param scfSelRecyclelingRanking
     * @return
     */
    @Override
    public IPage<ScfSelRecyclelingRanking> selectSelRecyclelingRanking(IPage<ScfSelRecyclelingRanking> page, ScfSelRecyclelingRanking scfSelRecyclelingRanking) {
        List<ScfSelRecyclelingRanking> scfSelRecyclelingRankings = baseMapper.selectSelRecyclelingRanking(page, scfSelRecyclelingRanking);
        for (ScfSelRecyclelingRanking selRecyclelingRanking : scfSelRecyclelingRankings) {
            // 利废成交吨数--单位换算万吨
            selRecyclelingRanking.setRecyclingWeight(unitConverter.convertToMetricTons(selRecyclelingRanking.getRecyclingWeight()));
            // 利废碳排放量--单位换算万吨
            selRecyclelingRanking.setCarbonEmissions(unitConverter.convertToMetricTons(selRecyclelingRanking.getCarbonEmissions()));
        }
        return page.setRecords(scfSelRecyclelingRankings);
    }

    /**
     * 供应商成交排名
     *
     * @param page
     * @param scfSelOwnerRanking
     * @return
     */
    @Override
    public IPage<ScfSelOwnerRanking> selectSelOwnerRanking(IPage<ScfSelOwnerRanking> page, ScfSelOwnerRanking scfSelOwnerRanking) {
        List<ScfSelOwnerRanking> scfSelOwnerRankings = baseMapper.selectSelOwnerRanking(page, scfSelOwnerRanking);
        for (ScfSelOwnerRanking selOwnerRanking : scfSelOwnerRankings) {
            // 供应商成交吨数--单位换算万吨
            selOwnerRanking.setOwnerWeight(unitConverter.convertToMetricTons(selOwnerRanking.getOwnerWeight()));
            // 供应商减少碳排放量--单位换算万吨
            selOwnerRanking.setCarbonEmissions(unitConverter.convertToMetricTons(selOwnerRanking.getCarbonEmissions()));
        }
        return page.setRecords(scfSelOwnerRankings);
    }

    @Override
    public ScfLineChart selectLineChart(  String[] orgCodes ) {
        return baseMapper.selectLineChart(Arrays.asList(orgCodes) );
    }

    @Override
    public List<ScfLineChartSection> selectLineChartSection( String[] orgCodes ) {
        return baseMapper.selectLineChartSection(Arrays.asList(orgCodes) );
    }
}
